package com.wf.course.web.controller.comm;

import com.aliyun.oss.internal.OSSUtils;
import com.utils.page.PageBean;
import com.wf.course.model.comm.Advertisement;
import com.wf.course.service.IAdvertisementService;
import com.wf.course.web.Constants;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.wf.course.web.OSSUtil;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

/**
 * @Auther: liu
 * @Date: 2018/6/12 18:59
 * @Description: 广告管理
 */
@Controller
@RequestMapping("advertisement")
public class AdvertisementController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IAdvertisementService advertisementService;

    @RequestMapping("list")
    public String list(@RequestParam(value = "pageNo", required = false) Integer pageNo,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                       Advertisement advertisement, Model model) {
        try {
            PageBean<Advertisement> pageBean = new PageBean<>(pageNo, pageSize);
            pageBean.setObj(advertisement);
            pageBean.setUrl("advertisement/list");
            advertisementService.list(pageBean);
            model.addAttribute("page", pageBean);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "comm/advertisementList";
    }

    @RequestMapping("pageLoad/{id}")
    public String pageLoad(@PathVariable("id") Integer id, Model model) {
        try {
            if (id != 0) {
                model.addAttribute("advertisement", advertisementService.getAdvertisementById(id));
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "comm/advertisementEdit";
    }

    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Advertisement advertisement) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            advertisement.setUpdatetime(new Timestamp(System.currentTimeMillis()));
            if (advertisement.getLowFile() != null && advertisement.getLowFile().getSize() > 0) {
                advertisement.setLowresolution(Constants.OSSURL +
                        OSSUtil.addFile(advertisement.getLowFile(), "head", Constants.IGM_BUCKET));
            }
            if (advertisement.getHightFile() != null && advertisement.getHightFile().getSize() > 0) {
                advertisement.setHightresolution(Constants.OSSURL +
                        OSSUtil.addFile(advertisement.getHightFile(), "head", Constants.IGM_BUCKET));
            }
            if (StringUtils.isNotBlank(advertisement.getDate())) {
                String[] date = advertisement.getDate().split(" - ");
                advertisement.setStartdate(new Timestamp(format.parse(date[0]).getTime()));
                advertisement.setEnddate(new Timestamp(format.parse(date[1]).getTime()));
            }
            if (advertisement.getId() != null) {
                advertisementService.update(advertisement);
            } else {
                advertisementService.add(advertisement);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "redirect:/advertisement/list";
    }

    @RequestMapping("delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        advertisementService.delete(id);
        return "redirect:/advertisement/list";
    }
}
